#pragma once
#include <vector>
#include <fstream>
#include <ctime>
#include <stdlib.h>
#include <stdio.h>

#include "constants.h"
using namespace std;

// 利用数据生成python脚本，通过system()函数调用python脚本可视化数据
template <typename IterPoint>
void plot_line(IterPoint start, IterPoint end)
{
    string py_file_path = OUTPUT_FILE.substr(0, OUTPUT_FILE.size() - 12) + "visualize_" + to_string(time(0)) + ".py";

    ofstream ofs(py_file_path, ios::out);
    ofs << "import matplotlib.pyplot as plt" << endl;
    ofs << "data = [";
    for (; start != end; ++start)
    {
        ofs << *start << ",";
    }
    ofs << "]" << endl;
    ofs << "plt.plot(data)" << endl;
    ofs << "plt.show()" << endl;
    ofs.close();

    string python_cmd = "python";
    if (char *python_cmd_env = getenv("PYTHON_CMD"))
    {
        python_cmd = string(python_cmd_env);
    }
    string cmd = python_cmd + " " + py_file_path;
    cout << "run [ " << cmd << " ]" << endl;
    system(cmd.c_str());
    remove(py_file_path.c_str());
}

template <typename IterPoint>
void plot_sorted(IterPoint start, IterPoint end)
{
    string py_file_path = OUTPUT_FILE.substr(0, OUTPUT_FILE.size() - 12) + "visualize_" + to_string(time(0)) + ".py";

    ofstream ofs(py_file_path, ios::out);
    ofs << "import matplotlib.pyplot as plt" << endl;
    ofs << "data = [";
    for (; start != end; ++start)
    {
        ofs << *start << ",";
    }
    ofs << "]" << endl;
    ofs << "plt.plot(sorted(data))" << endl;
    ofs << "plt.show()" << endl;
    ofs.close();

    string python_cmd = "python";
    if (char *python_cmd_env = getenv("PYTHON_CMD"))
    {
        python_cmd = string(python_cmd_env);
    }
    string cmd = python_cmd + " " + py_file_path;
    cout << "run [ " << cmd << " ]" << endl;
    system(cmd.c_str());
    remove(py_file_path.c_str());
}
